Convert WAV sampling rate and bit depth

This notebook converts WAV files in the specified 'directory' into FLAC format with the specified sampling 'rate' and bit 'depth'.

Variable definitions

directory – directory containing WAV files
rate – sampling rate (Hertz)
depth – bit depth (bits)


In [ ]:
directory = ""

In [2]:
rate = 48000

In [3]:
depth = 16

Import packages


In [1]:
import subprocess
import os.path
from os import listdir

Set directory


In [46]:
os.chdir(directory)
os.chdir(os.pardir)
converted_directory = os.path.abspath(os.curdir) + '/converted'
if os.path.exists(converted_directory):
    os.rmdir(converted_directory)
os.mkdir(converted_directory)

Find all WAV files in directory


In [47]:
WAV_files = [f for f in listdir(directory) if os.path.isfile(os.path.join(directory, f)) and f.split('.')[1] == 'WAV']

Convert all WAV files in directory


In [48]:
for filename in WAV_files:
    infilename = './clipped/' + filename
    outfilename = './converted/' + filename
    subprocess.check_output(["sox", infilename, "-r", str(rate), "-b", str(depth), outfilename])

Convert all WAV files in directory to flac files


In [49]:
for filename in WAV_files:
    infilename = './converted/' + filename
    subprocess.check_output(["flac", infilename])